﻿using Mario.DataFiles;
using Mario.GameEngine;
using Mario.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mario.UI
{
    public class ImageCreation
    {
        private const int brikWidth = 50;
        private const int brikHeight = 50;
        private const int groundY = 668;
        private const int worldHeight = 768;
        private const int worldWidth = 8196;
        private const int smallPipeHeight = 100;
        private const int middlePipeHeight = 150;
        private const int HighestPipeHeight = 200;

        /// <summary>
        /// Method to create image from xml
        /// </summary>
        /// <param name="xmlFile">Path to xml world file</param>
        /// <returns></returns>
        public Image CreateImage(string xmlFile)
        {
            Bitmap flag = new Bitmap(worldWidth, worldHeight);
            flag.MakeTransparent();
            Graphics flagGraphics = Graphics.FromImage(flag);
            object O = Resources.Resource1.BackGround;
            Image brick = (Image)O;
            XMLAccess access = new XMLAccess();
            GameObjects gameObject = access.GetOBjectsFromXML(xmlFile);

            object O2 = Resources.Resource1.Brick;
            Image brick2 = (Image)O2;
            object O1 = Resources.Resource1.Ground;
            Image brick1 = (Image)O1;
            object O3 = Resources.Resource1.BrickQ;
            Image brickQ = (Image)O3;
            object O4 = Resources.Resource1.BrickBrocken;
            Image brickB = (Image)O4;
            object O5 = Resources.Resource1.Castle;
            Image Castle = (Image)O5;
            object O6 = Resources.Resource1.Pipe_Smallest;
            Image PipeS = (Image)O6;
            object O7 = Resources.Resource1.Pipe_Middle;
            Image PipeM = (Image)O7;
            object O8 = Resources.Resource1.Pipe_Highest;
            Image PipeH = (Image)O8;
            using (Graphics g = Graphics.FromImage(flag))
            {
                g.DrawImage(brick, 0, 0);
                Rectangle fs = new Rectangle(0, 0, worldWidth, worldHeight);
                g.DrawImage(brick, fs);
                for (int i = 0; i < 1640; i++)
                {
                    for (int j = 0; j < 2; j++)
                    {
                        g.DrawImage(brick1, 0 + i * 50, 668 + j * 50);
                    }
                }
                for (int i = 0; i < gameObject.Bricks.Length; i++)
                {
                    g.DrawImage(brick2, gameObject.Bricks[i].XCoordinate, gameObject.Bricks[i].YCoordinate, gameObject.Bricks[i].Width, gameObject.Bricks[i].Height);
                }


                for (int i = 0; i < gameObject.BricksQ.Length; i++)
                {

                    g.DrawImage(brickQ, gameObject.BricksQ[i].XCoordinate, gameObject.BricksQ[i].YCoordinate, gameObject.BricksQ[i].Width, gameObject.BricksQ[i].Height);
                }


                for (int i = 0; i < gameObject.BricksB.Length; i++)
                {
                    g.DrawImage(brickB, gameObject.BricksB[i].XCoordinate, gameObject.BricksB[i].YCoordinate, gameObject.BricksB[i].Width, gameObject.BricksB[i].Height);
                }


                for (int i = 0; i < gameObject.Water.Length; i++)
                {
                    SolidBrush blueBrush = new SolidBrush(Color.LightSeaGreen);
                    g.FillRectangle(blueBrush, gameObject.Water[i].XCoordinate, gameObject.Water[i].YCoordinate, gameObject.Water[i].Width, gameObject.Water[i].Height);
                }
                g.DrawImage(Castle, gameObject.Castle.XCoordinate, gameObject.Castle.YCoordinate, gameObject.Castle.Width, gameObject.Castle.Height);

                for (int i = 0; i < gameObject.Pipes.Length; i++)
                {
                    if (gameObject.Pipes[i].Height < middlePipeHeight)
                    {
                        g.DrawImage(PipeS, gameObject.Pipes[i].XCoordinate, gameObject.Pipes[i].YCoordinate, gameObject.Pipes[i].Width, gameObject.Pipes[i].Height);
                    }
                    if (gameObject.Pipes[i].Height > smallPipeHeight)
                    {
                        g.DrawImage(PipeM, gameObject.Pipes[i].XCoordinate, gameObject.Pipes[i].YCoordinate, gameObject.Pipes[i].Width, gameObject.Pipes[i].Height);
                    }
                    if (gameObject.Pipes[i].Height > middlePipeHeight)
                    {
                        g.DrawImage(PipeH, gameObject.Pipes[i].XCoordinate, gameObject.Pipes[i].YCoordinate, gameObject.Pipes[i].Width, gameObject.Pipes[i].Height);
                    }

                }
            }
            return flag;
        }
    }
}
